﻿using FB.Mvc.Controllers;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace FCDB.Web.Controllers
{
     [FB.Membership.Annotations.AuthorizeOnly]
    public class WorkFormApplyController : BaseController
    {
        //
        // GET: /WorkFormApply/
        private WFWorkFlowRepository respository = new WFWorkFlowRepository();
        public ActionResult Index()
        {
            long WorkFlowTypeID=0;
            long.TryParse(Request["typeId"],out WorkFlowTypeID);
            string keyword = Request["keyword"];
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                IList<WfProcessEx> list = respository.GetPagedList(skip, take, WorkFlowTypeID, keyword, out total);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json; 
            }
            IList<WfProcessType> WorkFlowTypeList = respository.GetWorkFlowType();
            ViewBag.WorkFlowTypeList = WorkFlowTypeList;
            return View(); 
        }

        public ActionResult MyWorkFormApply()
        {
            long WorkFlowTypeID = 0;
            long.TryParse(Request["typeId"], out WorkFlowTypeID);
            string keyword = Request["keyword"];
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                List<WfProcessEx> list = respository.GetMyPagedList(skip, take, WorkFlowTypeID, this.UserID, keyword, out total);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }
            IList<WfProcessType> WorkFlowTypeList = respository.GetWorkFlowType();
            ViewBag.WorkFlowTypeList = WorkFlowTypeList;
            return View();
        }
         public ActionResult addForm()
        {
            IList<MyWfProcess> type = respository.GetMyProcess(this.UserID);
            ViewBag.MyWfProcess = type;
            IList<WfProcess> AllWfProcess = respository.GetAllProcess();
            ViewBag.AllWfProcess = AllWfProcess;
            return View(type);
        }

         public ActionResult SaveMyWorkForm(string modelString)
         {

             JavaScriptSerializer jsonConverter = new JavaScriptSerializer();
             IList<Guid> model = jsonConverter.Deserialize<IList<Guid>>(modelString);
             RtnMsg msg = new RtnMsg() { Status = 1 };
             if (model == null)
             {
                 msg.Status = 3;
                 return Json(msg);
             }
             // 删除以前的选择的
             IList<MyWfProcess> delete = respository.GetMyProcess(this.UserID);
             foreach (var i in delete)
             {
                 respository.Delete<MyWfProcess>(i.ID);
             }
             foreach (var i in model)
             {
                 MyWfProcess New = new MyWfProcess();
                 New.ID = IDCreateLogic.GenerateID("MyWfProcess", "我的常用流程");
                 New.UserID = this.UserID;
                 New.ProcessGUID = i;
                 respository.Add(New);
             }
             return Json(msg);
         }
    }
}
